<template>
   <div class="m-window">
        <el-row :gutter="10" style="margin:8px auto;">
            <el-col :span="2">
                <el-button type="primary" @click="backToMyList">返回</el-button>
            </el-col>
            <el-col :span="2" v-show="pageSourceCode != 'myProcessed'">
                <el-button type="primary" @click="sendBack">退回</el-button>
            </el-col>
            <el-col :span="2" v-show="pageSourceCode != 'myProcessed'">
                <el-button type="primary" @click="pass">通过</el-button>
            </el-col>
        </el-row>
       <browse-contract
                :account="account"
                :applyId="applyId"
                :contentColHeight="contentColHeight"/>
        <el-row style="margin-top:20px;">
            <el-col>
                <el-form :model="lawWorksBForm" ref="lawWorksBForm" class="lawWorksBForm">
                    <el-form-item prop="isNakedin">
                        <el-checkbox label="已中登" v-model="lawWorksBForm.isNakedin" :disabled="pageSourceCode == 'myProcessed'"/> 
                        <div class="files-col">
                            <a href="javascript:;" v-for="(v,i) in zdExcels" :key="i" :title="v.orgName" @click="fileDownload(v.orgName,v.fileName)">
                                <i class="icon el-icon-download"></i>
                                <span>{{v.orgName}}</span></a>
                        </div>
                    </el-form-item>
                    <el-form-item prop="note" style="margin-top:-10px;">
                        <label>退回原因 &nbsp;<i class="el-icon-warning"></i></label>
                        <el-input type="textarea" v-model.trim="lawWorksBForm.note" placeholder="退回原因" :disabled="pageSourceCode == 'myProcessed'" />
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>
<style lang="less" scoped>
    .m-window{
        overflow: auto;
        overflow-x: hidden;
        .lawWorksBForm{
            .files-col{
                display: flex;
                flex-direction: row;
                flex-wrap:wrap;
                a{
                    display:block;
                    width: 250px;
                    height: 60px;
                    line-height: 60px;
                    margin-right: 10px;
                    border: 1px solid #eee;
                    color: #818181;
                    box-sizing: border-box;
                    text-decoration:none;
                    padding-right: 5px;
                    overflow:hidden;
                    white-space: nowrap;
                    i.icon{
                        width: 60px;
                        text-align: center;
                        height: 60px;
                        line-height: 60px;
                        border-right: 1px solid #eee;
                        box-sizing: border-box;
                    }
                    span{
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        float: right;
                        width: 180px;
                        box-sizing: border-box;
                    }
                }
            }
        }
    }
</style>
<script>
import { showAlert } from "lesso-common/public/utils"
import { mapState,mapActions } from 'vuex'
import { download } from '@/util/fileDownload.js'

export default {   
    props:['pageSourceCode'],
    data(){
        return {
            contentColHeight:550,
            checkTab:'',
            lawWorksBForm:{
                isNakedin:false,//是否中登
                result:1, //0 未到达、1 待审批、2 通过 、3 退回
                note:'' //退回原因
            },
            examinePass:2, //2 通过
            examineNotgo:3, //3 退回
            linkName:'act_fin_review_reg', //流程环节名称
            account:'',
            applyId:'',
            flowStepCode:0, //步骤状态
            zdExcels:[]
        }
    },
    created(){
        this.account = this.supplyChainFinance.role.eSignAccount//使用供应商的电子签账号，这样才只能预览合同
        this.applyId = this.supplyChainFinance.role.ID
        this.getZDExcel()
    },
    computed: {
        ...mapState(['supplyChainFinance'])
    },
    methods:{
        ...mapActions([
            'changeTOdo',
            'changeMyProcessedCTCode'
        ]),       
        async commonUpdate(){
            let that = this
            let parameter = {
                applyId:that.applyId,
                name:that.linkName,
                result:that.lawWorksBForm.result,
                note:that.lawWorksBForm.note,
            }            
            await that.$http.commonUpdate(parameter).then(({data})=>{
                if(data.code == 200){
                     showAlert({
                        msg: `操作成功！`,
                        type: 1
                    })
                    that.changeTOdo({isMyToDoList:that.flowStepCode})
                }else {
                    this.$alert(`操作失败：${data.info}`, {
                        confirmButtonText: "确定",
                        customClass: "alertFailure",
                        callback:()=>{
                            if(that.lawWorksBForm.isNakedin){
                                that.lawWorksBForm.isNakedin = false
                                that.signature()
                            }
                        }
                    })
                }
            })
            .catch((error)=>{
                this.$alert("查询失败，请检查网络！", {
                    confirmButtonText: "确定",
                    customClass: "alertFailure"
                })
            })
        },
        async signature(){
            let that = this
            await that.$http.tblFinSrmApplyInfo({
                'id':that.applyId,
                'isNakedin':that.lawWorksBForm.isNakedin? 1:0
            }).then(({data})=>{
                if(data.code == 200){
                    that.commonUpdate()          
                }else{
                    showAlert({
                        msg: `操作失败：${data.info}`,
                        type: 0
                    })
                }
            }).catch((error)=>{
                this.$alert("查询失败，请检查网络！", {
                    confirmButtonText: "确定",
                    customClass: "alertFailure"
                })
            })
        },
        //获取中登文件
        async getZDExcel(){
            let that = this
            await that.$http.createZDExcel({
                'applyId':that.applyId
            }).then(({data})=>{
                console.log('data:',data)
                if(data.code == 200){
                    that.zdExcels = data.rows || []
                }else{
                    showAlert({
                        msg: `操作失败：${data.info}`,
                        type: 0
                    })
                }
            }).catch((error)=>{
                this.$alert("查询失败，请检查网络！", {
                    confirmButtonText: "确定",
                    customClass: "alertFailure"
                })
            })
        },
        pass(){
            if(!this.lawWorksBForm.isNakedin){
                showAlert({
                    msg: "请勾选“已中登”选项",
                    type: 2
                })
                return
            }
            this.$confirm('是否确认通过？')
                .then(_ => {
                    this.lawWorksBForm.result = this.examinePass
                    this.lawWorksBForm.note = ''
                    this.flowStepCode = 0
                    this.signature()
                })
                .catch(_ => {})
        },
        sendBack(){
            if(!this.lawWorksBForm.note){
                 showAlert({
                    msg: "请填写退回原因",
                    type: 2
                })
                return
            }
            this.$confirm('是否确认退回？').then(_ => {
                this.lawWorksBForm.result = this.examineNotgo
                this.lawWorksBForm.isNakedin = false
                this.flowStepCode = 0                
                this.commonUpdate()
            })
        },
        tabsChange(tab){
            this.checkTab = tab.name
        },
        backToMyList(){
            if(this.pageSourceCode == 'myProcessed'){
                this.changeMyProcessedCTCode('')
            }else{
                this.changeTOdo({isMyToDoList:0})
            }             
        },
        fileDownload(filename,url){
           this.$http.getFullFilePath({
            'url': url
            }).then(({ data }) => {
                if (data.code == 200) {
                    download(filename,data.rows[0].fileName)
                } else {
                    showAlert({
                        msg: `操作失败：${data.info}`,
                        type: 0
                    })
                }
            })
            .catch((error) => {
                showAlert({
                    msg: `查询失败，请检查网络！`,
                    type: 0
                })
            })
       },
    }
}
</script>
